package view {
	import flash.display.Sprite;
	import flash.filters.GlowFilter;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	import flash.text.TextFormat;
	import gs.TweenLite;
	import model.Player;
	
	/**
	 * ...
	 * @author flyingSnow
	 */
	public final class ScoreLine extends Sprite implements IPlayerView{
		private var _colorIndex:int;
		private var _lineLayer:Sprite;
		private var _colorLayer:Sprite;
		private var _maskLayer:Sprite;
		private var _tfScore:TextField;
		
		public function ScoreLine(colorIndex:int) {
			this._colorIndex = colorIndex;
			drawGrids();
		}
		
		/* INTERFACE view.IPlayerView */
		
		public function update(player:Player):void {
			var maskWidth:int;
			if (player.score <= 89) {
				maskWidth = 6 * player.score;
			}else {
				maskWidth = 600-(109 - player.score) * 16;
			}
			_tfScore.text = player.score+"";
			TweenLite.to(_maskLayer, 0.5, {width:maskWidth} );
			TweenLite.to(_tfScore, 0.5, {x:maskWidth } );
		}
		
		private function drawGrids():void {
			_colorLayer = new Sprite();
			_colorLayer.graphics.beginFill(Square.COLORS[_colorIndex]);
			_colorLayer.graphics.drawRect(0, 0, 89 * 6, 12);
			_colorLayer.graphics.endFill();
			
			_colorLayer.graphics.beginFill(0xFF8040);
			_colorLayer.graphics.drawRect(89 * 6, 0, 48, 12);
			_colorLayer.graphics.endFill();
			
			_colorLayer.graphics.beginFill(0xC81102);
			_colorLayer.graphics.drawRect(89 * 6+48, 0, 16, 12);
			_colorLayer.graphics.endFill();
			addChild(_colorLayer);
			
			//
			_lineLayer = new Sprite();
			_lineLayer.graphics.lineStyle(1, 0);
			
			for (var i:int = 0; i < 89; i++ ) {
				_lineLayer.graphics.drawRect(i * 6, 0, 6, 12);
			}
			_lineLayer.graphics.drawRect(89*6, 0, 48, 12);
			_lineLayer.graphics.drawRect(89 * 6 + 48, 0, 16, 12);
			
			_lineLayer.alpha = 0.5;
			addChild(_lineLayer);
			
			//
			_maskLayer = new Sprite();
			_maskLayer.graphics.beginFill(0);
			_maskLayer.graphics.drawRect(0, 0, 10, 12);
			_colorLayer.mask = _maskLayer;
			addChild(_maskLayer);	
			
			//
			_tfScore = new TextField();
			_tfScore.selectable = false;
			_tfScore.defaultTextFormat = new TextFormat("Comic Sans MS", 14, Square.COLORS[_colorIndex]);
			_tfScore.autoSize = TextFieldAutoSize.LEFT;
			_tfScore.text = "";
			_tfScore.y = -20;
			_tfScore.filters = [new GlowFilter(0,0.8,4,4,1)];
			addChild(_tfScore);
		}
		
	}

}